﻿using Swimj.DomainLib.Entities;

namespace BaseData.DomainLib.AggregatesModel.SaleOrderAggregate;

public class SaleOrder : AuditableAggregateRoot<int>
{
    /// <summary>
    /// 销售部单号
    /// </summary>
    public string? Code { get; set; }

    public string? Name { get; set; }

    /// <summary>
    /// 所属客户
    /// </summary>
    public int? CustomerId { get; set; }

    public string? CustomerOrderCode { get; set; }

    /// <summary>
    /// 合同单号
    /// </summary>
    public string? ContractOrderCode { get; set; }

    /// <summary>
    /// 交付日期
    /// </summary>
    public DateTime? DeliveryTime { get; set; }

    /// <summary>
    /// 单据类型
    /// </summary>
    public int SaleOrderType { get; set; }

    /// <summary>
    /// 单据状态
    /// </summary>
    public int SaleOrderStatus { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }


    public readonly List<SaleOrderDetail> _saleOrderDetails;

    public IReadOnlyCollection<SaleOrderDetail> SaleOrderDetails => _saleOrderDetails;


    public SaleOrder()
    {
        _saleOrderDetails = new List<SaleOrderDetail>();
    }

    public SaleOrder(string code, string name, string? description, int? customerId, string? customerOrderCode,
        string? contractOrderCode,
        DateTime? deliveryTime, int saleOrderType) : this()
    {
        Code = code;
        Name = name;
        Description = description;
        CustomerId = customerId;
        CustomerOrderCode = customerOrderCode;
        ContractOrderCode = contractOrderCode;
        DeliveryTime = deliveryTime;
        SaleOrderType = saleOrderType;
    }

    public void AddItem(int itemMaterialId, decimal itemExcludingTaxPrice, decimal itemIncludingTaxPrice,
        decimal itemTax, DateTime itemDeliveryTime, decimal itemQuantity)
    {
        var saleOrderDetail = new SaleOrderDetail(itemMaterialId, itemExcludingTaxPrice, itemIncludingTaxPrice,
            itemTax, itemQuantity, itemDeliveryTime);
        _saleOrderDetails.Add(saleOrderDetail);
    }
}